package com.atguigu.guli.service.edu.controller;


import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.TeacherQuery;
import com.atguigu.guli.service.edu.feign.OssFeignService;
import com.atguigu.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-01-04
 */
@RestController
@CrossOrigin
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    OssFeignService ossFeignService;
    @GetMapping("/list")
    @ApiOperation(value = "查询所有教师")
    public R test(){
        List<Teacher> list = teacherService.list();
        return R.ok().data("items",list);
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "根据id删除教师")
    public R removeTeacherById(@PathVariable String id){
        teacherService.removeAvatarById(id);
        boolean b = teacherService.removeById(id);

        System.out.println(b);
        if (b){

            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }
    }

    @GetMapping("/list/{page}/{limit}")
    public R listpage(@ApiParam(value = "当前页码",required = true)@PathVariable Long page,
                      @ApiParam(value="每页个数",required = true)@PathVariable Long limit,
                      @ApiParam(value="查询条件",required = true)TeacherQuery teacherQuery
                      ){
        IPage<Teacher> listpage = teacherService.listpage(page, limit, teacherQuery);
        return R.ok().data("pageModel",listpage);
    }
    @ApiOperation("新增讲师")
    @PostMapping("save")
    public  R save(@RequestBody Teacher teacher){
           boolean result = teacherService.save(teacher);
           if (result){
               return R.ok().message("新增成功");
           }else {
               return R.error().message("新增失败");
           }
    }

    @ApiOperation("更新讲师")
    @PutMapping("update")
    public  R update(@RequestBody Teacher teacher){
        boolean update = teacherService.updateById(teacher);
        if (update){
            return R.ok().message("修改成功");
        }else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id查询教师")
    @GetMapping("/queryTeacherOne/{id}")
    public R queryTeacherOne(@PathVariable String id){
        Teacher byId = teacherService.getById(id);
        return R.ok().data("item",byId);
    }
    @ApiOperation("根据id批量删除")
    @DeleteMapping("/deleteTeachers")
    public R deleteTeacher(@RequestBody List<String> idList){
        boolean b = teacherService.removeByIds(idList);
        for (String s : idList) {
            teacherService.removeAvatarById(s);
        }
        if (b){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("删除失败");
        }
    }

    @GetMapping("/test")
    public R test1(){
        ossFeignService.test();
        return R.ok();
    }
}

